﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;

namespace Tireguit.Nmon.Parser.Information
{
    public class BBBPElement:NmonInfoElement
    {
        protected new ILog _log=LogManager.GetLogger(typeof(BBBPElement));
        protected IDataParameter _numberline = null;
        protected IDataParameter _cde = null;
        protected IDataParameter _infoline = null;
        public string NumberLine {get;protected set;}
        public string Cde { get; protected set; }
        public BBBPElement(IDbConnection connection)
        {
            Balise = BaliseType.BBBP;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq="INSERT INTO "+NmonTables.TablesDictionnary.Instance["BBBP"]+" ([Numberline],[Command],[Infoline]) VALUES (@Numberline,@Command,@Infoline)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _numberline = Command.CreateParameter();
            _numberline.ParameterName = "@Numberline";
            _numberline.DbType = DbType.String;
            _cde = Command.CreateParameter();
            _cde.ParameterName = "@Command";
            _cde.DbType = DbType.String;
            _infoline = Command.CreateParameter();
            _infoline.ParameterName = "@Infoline";
            _infoline.DbType = DbType.String;
        }
        public BBBPElement():this(null){}
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _numberline.Value = NumberLine;
                _cde.Value = Cde;
                _infoline.Value = InfoLine;
                Command.Parameters.Add(_numberline);
                Command.Parameters.Add(_cde);
                Command.Parameters.Add(_infoline);

                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (elements[0] != "BBBP")
                {
                    _log.Error("Not BBBP Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif

                NumberLine = elements[1];
                Cde = elements[2];
                InfoLine=string.Empty;
                bool passed=false;
                for (int i=3;i<elements.Length;i++)
                {
                    InfoLine += elements[i] + ",";
                    passed = true;
                }
                if (passed)
                {
                    InfoLine = InfoLine.Remove(InfoLine.Length - 1);
                }
                
                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
